package main

import (
	"flag"
	kinit "longblog/work/base/initialize"
	kroute "longblog/work/base/route"
	kblog "longblog/work/control/blog"
	kcms "longblog/work/control/cms"
	khome "longblog/work/control/home"
	ktest "longblog/work/control/test"
	"runtime"

	kruntime "longblog/framework/tools/runtime"
)

func main() {
	types := flag.String("t", "", "启动类型，空：正常，sql：执行sql方法生成，api：启动程序")
	flag.Parse()

	if *types == "api" {
		kruntime.MainGetPanic(func() {
			kruntime.Pid("api.pid")

			if runtime.GOOS != "windows" {
				kinit.InitLog("api")
			}

			kinit.InitMysql()
			kinit.InitRedis()

			port, _ := kinit.Conf.GetInt("server", "port")
			r := kroute.NewRouteStruct(port)
			r.SetMiddleware(kroute.MiddlewareCrossDomain())
			r.SetMiddleware(kroute.MiddlewareLoggerWithWriter(kinit.LogError))

			r.Load(kcms.NewCms())
			r.Load(ktest.NewTest())
			r.Load(kblog.NewArticleType())
			r.Load(kblog.NewArticle())
			r.Load(kblog.NewUpload())

			r.Run()
		})
	}

	if *types == "home" {
		kruntime.MainGetPanic(func() {
			kruntime.Pid("home.pid")

			if runtime.GOOS != "windows" {
				kinit.InitLog("home")
			}

			kinit.InitMysql()
			kinit.InitRedis()

			port, _ := kinit.Conf.GetInt("homeserver", "port")
			r := kroute.NewRouteStruct(port)
			r.SetMiddleware(kroute.MiddlewareCrossDomain())
			r.SetMiddleware(kroute.MiddlewareLoggerWithWriter(kinit.LogError))

			r.Load(khome.NewHome())

			r.Run()
		})
	}
}
